import axios from 'axios'
// 返回值是response.data
export default function ajax(url, data={},type='GET') {
   
    return new Promise((resolve,reject)=>{
         let promise
        if(type==='Get'){
            // 准备url query参数数据
            let dataStr=''

            Object.keys(data).forEach(key=>{
                dataStr+=key+'='+ data[key]+'&'
            })
            if(dataStr!==''){
               dataStr=dataStr.substring(0,dataStr,lastIndexOf('&'))
               url=url+'?'+dataStr
            }
            promise=axios.get(url)
        }else{
            promise=axios.post(url,data)
        }
    promise.then(response=>{
        resolve(response.data)
    }).catch(error=>{
        reject(error)
    })
})
}